﻿@page "/demo/weather"
@attribute [StreamRendering]
@inherits BaseComponent
@attribute [Menu(AppConstant.Demo, "天气", "cloud", 3)]

<PageTitle>天气</PageTitle>

<TabPage PageName="天气" Query="model">
    <TabContent>
        <TabPane Key="Order" Tab="北京">
            <KTable Model="model" />
        </TabPane>
        <TabPane Key="Distr" Tab="上海">
            <KTable Model="model" />
        </TabPane>
    </TabContent>
    <TabRight>
        <KToolbar Model="model.Toolbar" />
    </TabRight>
</TabPage>

@code {
    private TableModel<WeatherForecast> model;

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        model = new TableModel<WeatherForecast>(this, TableColumnMode.Property);
        model.ShowPager = true;
        model.Name = "天气";
        model.OnQuery = OnQueryWeatherForecastsAsync;
        model.Toolbar.AddAction(nameof(New));
    }

    public void New()
    {
        model.NewForm(d =>
        {
            UI.Alert($"Summary={d.Summary}");
            return Known.Result.SuccessAsync("");
        });
    }

    private async Task<PagingResult<WeatherForecast>> OnQueryWeatherForecastsAsync(PagingCriteria criteria)
    {
        await Task.Delay(500);

        var startDate = DateTime.Now;
        var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
        var forecasts = Enumerable.Range(1, criteria.PageSize).Select(index => new WeatherForecast
        {
            Date = startDate.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = summaries[Random.Shared.Next(summaries.Length)]
        }).ToList();
        return new PagingResult<WeatherForecast>(100, forecasts);
    }
}